package com.glsc.ngateway.oaflow.controller;

import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeBaseInfoDto;
import com.glsc.ngateway.common.api.oaflow.resp.OaflowResponse;
import com.glsc.ngateway.oaflow.service.OaSyncService;
import com.glsc.ngateway.oaflow.service.xirtrade.TOaFlowXirTradeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;


/**
 * @Author: xc
 * @Date: 2021/10/20
 * @Desc: 作业信息管理
 */
@RestController
@RequestMapping("/dolphinScheduler")
public class DolphinSchedulerController {

    private static Logger logger = LoggerFactory.getLogger(DolphinSchedulerController.class);

    @Autowired
    private OaSyncService oaSyncService;

    @Autowired
    private TOaFlowXirTradeService TOaFlowXirTradeInfoService;

    @GetMapping("/checkSysnLog")
    public OaflowResponse<Void> checkSysnLog() {
        try {
            logger.info("开始执行任务：checkSysnLog");
            oaSyncService.checkSysnLog();
        } catch (Exception e) {
            return OaflowResponse.failMsg("失败" + e.getMessage());
        }
        return OaflowResponse.successMsg("成功");
    }

    @GetMapping("/syncXirTrade")
    public OaflowResponse<Void> syncXirTrade(@RequestParam(value = "date", required = false) String date,
                                             @RequestParam(value = "unPushFlow", required = false) boolean unPushFlow,
                                             @RequestParam(value = "type", required = false) String type) {
        try {
            date = StringUtils.isBlank(date) ? LocalDate.now().toString() : date;
            List<TOaFlowXirTradeBaseInfoDto> res = TOaFlowXirTradeInfoService.syncXirTrade(date, unPushFlow, type);
            return OaflowResponse.successMsg("操作成功，已同步：" + res.size() + "笔");
        } catch (Exception e) {
            e.printStackTrace();
            return OaflowResponse.failMsg("失败:" + e.getMessage());
        }
    }
}
